package com.cheuckfei.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheuckfei.user.constants.JwtClaimsConstant;
import com.cheuckfei.user.mapper.UserMapper;
import com.cheuckfei.user.properties.JwtProperties;
import com.cheuckfei.user.service.UserService;
import com.cheuckfei.user.utils.JwtUtil;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.dtos.Result;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dto.LoginDto;
import com.heima.model.user.pojo.ApUser;
import com.heima.utils.common.AppJwtUtil;
import io.jsonwebtoken.Jwt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, ApUser> implements UserService {
    @Autowired
    private JwtProperties jwtProperties;

    @Override
    public ResponseResult<String> login(LoginDto loginDto) {
//        不为空在判断
        if (StringUtils.isNotBlank(loginDto.getPhone()) && StringUtils.isNotBlank(loginDto.getPassword())) {
//            根据电话号码查询  user
            ApUser apUser = getOne(Wrappers.<ApUser>lambdaQuery().eq(ApUser::getPhone, loginDto.getPhone()));
            if (apUser == null || apUser.equals("")){
               return   ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"用户不存在");
            }
//            查询salt
            String salt = apUser.getSalt();
//            比对密码
            if (apUser.getPassword().equals
                    (DigestUtils.md5DigestAsHex((loginDto.getPassword()+salt).getBytes())))
//            if (loginDto.getPassword().equals
//                    (loginDto.getPassword()))
            {
                Integer id = apUser.getId();
                Map<String, Object> claim = new HashMap<>();
                claim.put(JwtClaimsConstant.USER_ID, id);
//                生成token
                String token = JwtUtil.createJWT(
                        jwtProperties.getAdminSecretKey(),
                        jwtProperties.getAdminTtl(),
                        claim
                );
//                return Result.success(token);
                //1.3 返回数据  jwt
                Map<String, Object> map = new HashMap<>();
                map.put("token", token);
                apUser.setSalt("");
                apUser.setPassword("");
                map.put("user", apUser);
                return ResponseResult.okResult(map);
            }
//            return ResultP.error("密码错误");
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        } else {
//           游客登录
            Map<String, Object> claim = new HashMap<>();
            claim.put(JwtClaimsConstant.USER_ID, 0L);
//                生成token
            String token = JwtUtil.createJWT(
                    jwtProperties.getAdminSecretKey(),
                    jwtProperties.getAdminTtl(),
                    claim
            );
//            return Result.success(token);
            //2.游客  同样返回token  id = 0
            Map<String, Object> map = new HashMap<>();
            map.put("token", token);
            return ResponseResult.okResult(map);
        }
    }
}
